
<h1><span class="yiyi-st" id="yiyi-12">numpy.indices</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.indices.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.indices.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.indices"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.</code><code class="descname">indices</code><span class="sig-paren">(</span><em>dimensions</em>, <em>dtype=&lt;type &apos;int&apos;&gt;</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/core/numeric.py#L1936-L2007"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">返回表示网格索引的数组。</span></p>
<p><span class="yiyi-st" id="yiyi-15">计算数组，其中子数组包含仅沿对应轴变化的索引值0,1，...。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-16">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-17"><strong>尺寸</strong>：整数序列</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-18">网格的形状。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-19"><strong>dtype</strong>：dtype，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-20">结果的数据类型。</span></p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-21">返回：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-22"><strong>网格</strong>：ndarray</span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-23">The array of grid indices, <code class="docutils literal"><span class="pre">grid.shape</span> <span class="pre">=</span> <span class="pre">(len(dimensions),)</span> <span class="pre">+</span> <span class="pre">tuple(dimensions)</span></code>.</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-24">也可以看看</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-25"><a class="reference internal" href="numpy.mgrid.html#numpy.mgrid" title="numpy.mgrid"><code class="xref py py-obj docutils literal"><span class="pre">mgrid</span></code></a>，<a class="reference internal" href="numpy.meshgrid.html#numpy.meshgrid" title="numpy.meshgrid"><code class="xref py py-obj docutils literal"><span class="pre">meshgrid</span></code></a></span></p>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-26">笔记</span></p>
<p><span class="yiyi-st" id="yiyi-27">The output shape is obtained by prepending the number of dimensions in front of the tuple of dimensions, i.e. if <em class="xref py py-obj">dimensions</em> is a tuple <code class="docutils literal"><span class="pre">(r0,</span> <span class="pre">...,</span> <span class="pre">rN-1)</span></code> of length <code class="docutils literal"><span class="pre">N</span></code>, the output shape is <code class="docutils literal"><span class="pre">(N,r0,...,rN-1)</span></code>.</span></p>
<p><span class="yiyi-st" id="yiyi-28">子阵列<code class="docutils literal"><span class="pre">grid[k]</span></code>包含沿着<code class="docutils literal"><span class="pre">k-th</span></code></span><span class="yiyi-st" id="yiyi-29">显式：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">grid</span><span class="p">[</span><span class="n">k</span><span class="p">,</span><span class="n">i0</span><span class="p">,</span><span class="n">i1</span><span class="p">,</span><span class="o">...</span><span class="p">,</span><span class="n">iN</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">ik</span>
</pre></div>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-30">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">grid</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">indices</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">grid</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(2, 2, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">grid</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>        <span class="c1"># row indices</span>
<span class="go">array([[0, 0, 0],</span>
<span class="go">       [1, 1, 1]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">grid</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>        <span class="c1"># column indices</span>
<span class="go">array([[0, 1, 2],</span>
<span class="go">       [0, 1, 2]])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-31">索引可以用作数组的索引。</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">row</span><span class="p">,</span> <span class="n">col</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">indices</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">[</span><span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">]</span>
<span class="go">array([[0, 1, 2],</span>
<span class="go">       [4, 5, 6]])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-32">注意，在上面的例子中，使用<code class="docutils literal"><span class="pre">x [：2，</span> <span class="pre">：3]</span></code>直接提取所需的元素更为直接。</span></p>
</dd></dl>
